package com.example.datadashboard.controller;

import com.example.datadashboard.common.Result;
import com.example.datadashboard.entity.DataSource;
import com.example.datadashboard.service.DataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/datasource")
public class DataSourceController {
    @Autowired
    private DataSourceService dataSourceService;

    @GetMapping("/{id}")
    public Result<DataSource> getById(@PathVariable Long id) {
        return Result.success(dataSourceService.getById(id));
    }

    @GetMapping("/user/{userId}")
    public Result<List<DataSource>> getUserDataSources(@PathVariable Long userId) {
        return Result.success(dataSourceService.getUserDataSources(userId));
    }

    @PostMapping("/test")
    public Result<Boolean> testConnection(@RequestBody DataSource dataSource) {
        return Result.success(dataSourceService.testConnection(dataSource));
    }

    @PostMapping
    public Result<Boolean> save(@RequestBody DataSource dataSource) {
        return Result.success(dataSourceService.save(dataSource));
    }

    @PutMapping
    public Result<Boolean> update(@RequestBody DataSource dataSource) {
        return Result.success(dataSourceService.updateById(dataSource));
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(dataSourceService.removeById(id));
    }
}
